package pers.youqu.item.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.youqu.item.dto.CategoryDTO;
import pers.youqu.item.service.CategoryService;

import java.util.List;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/4 11:42
 * @Description TODO
 */
@RestController
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 根据分类id，查询商品分类信息
     * @param id 分类的id
     * @return 结果
     */
    @GetMapping("/category/{id}")
    public ResponseEntity<CategoryDTO> findCategoryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(categoryService.findCategoryById(id));
    }

    /**
     * ResponseEntity<List<CategoryDTO>>：包含状态码和响应结果集的封装类
     * SpringMVC将：1，2，3请求参数字符串转成的List集合
     *
     * 根据多个id，查询分类信息
     * @param ids 分类的id
     * @return 多条分类结果
     */
    @GetMapping("/category/list")
    public ResponseEntity<List<CategoryDTO>> findCategoryByListId(@RequestParam("ids")List<Long> ids) {
        return ResponseEntity.ok(categoryService.findCategoryByListId(ids));
    }

    /**
     * 根据品牌id，查询商品分类的集合
     * @param id 品牌id
     * @return 多条分类结果集
     */
    @GetMapping("/category/of/brand")
    public ResponseEntity<List<CategoryDTO>> findCategoryByBrandId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(categoryService.findCategoryByBrandId(id));
    }

    /**
     * 根据父类目id，查询子类目的集合
     * @param pid 父分类的id
     * @return 多条分类的结果集
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<CategoryDTO>> findCategoryByParentId(@RequestParam("pid") Long pid) {
        return ResponseEntity.ok(categoryService.findCategoryByParentId(pid));
    }

}
